from timesheet.models import *

from datetime import timedelta
from datetime import date

"""
    In this file some functions which have been taken out
    used in views.
    
"""

def find_delete_time(times, post):
    for time in times:
        if ('delete' + ' ' + str(time[4]) in post):
            return time[4]
    return False


def summation_time(some_list):
    val = 0
    for i in some_list:
        val = val + i[1]
        
    return val


def get_dates(num_week, begin_day):
    list_day = []
    for i in xrange(num_week):
        for j in xrange(7):
            lst1 = []
            day = begin_day + timedelta(days=(i*7 + j))
            weekday = day.strftime('%a')
            lst1.append(day)
            lst1.append(weekday)
            list_day.append(lst1)

    return list_day


def convert_data_times(times, list_day):
    new_list_data = []
    projects = []
    
    for i in times:
        try:
            projects.index(i[1])
        except(ValueError):
            pr_list = []
            pr_list.append(i[1])
            projects.append(i[1])
            ac_list = []
            list1 = []
            for j in times:
                if j[1] == pr_list[0]:
                    list1.append(j)
            list2 = []
            list3 = []
            for j in list1:
                n = 0
                l = 0
                q = 0
                t = 0
                for k in list1:
                    q += 1
                    if ((k != j) and (k[0] == j[0]) and (k[1] == j[1])
                        and (k[2] == j[2])):
                        try:
                            list3.index(j)
                            t += 1
                        except(ValueError):
                            list3.append(k)
                            if l == 0:
                                n += k[3] + j[3]
                            else:
                                n += k[3]
                            l += 1
                    if ((q == (len(list1))) and (l == 0) and (t == 0)):
                        n = j[3]
                if t == 0:
                    x = []
                    x.append(j[0])
                    x.append(j[2])
                    x.append(n)
                    x.append(j[4])
                    x.append(j[5])
                    list2.append(x)
            activities = []
            for j in list2:
                try:
                    activities.index(j[1])
                except(ValueError):
                    activities.append(j[1])
            for j in activities:
                sub_list = []
                list3 = []
                sub_list.append(j)
                for k in list_day:
                    l = 0
                    for z in list2:
                        if ((k[0] == z[0]) and (j == z[1])):
                            list3.append([k[0], z[2], z[4]])
                            l += 1
                    if l == 0:
                        list3.append([k[0], '', 0])
                sub_list.append(list3)
                ac_list.append(sub_list)
            pr_list.append(ac_list)
            new_list_data.append(pr_list)                
                    
    return new_list_data


def sum_total_time_down(data):
    total_time = []
    for i in xrange(0, 7):
        n = 0
        for j in data:
            for k in j[1]:
                if k[1][i][1] == '':
                    n += 0
                else:
                    n += k[1][i][1]
        if n == 0:
            total_time.append('')
        else:
            total_time.append(n)

    return total_time


def sum_total_time_right(data):
    total_time = []
    for i in data:
        for j in i[1]:
            n = 0
            for k in j[1]:
                try:
                    n += float(k[1])
                except(ValueError):
                    pass
            total_time.append(n)

    return total_time


def sum_total_time(total_time_down):
    n = 0
    for i in total_time_down:
        try:
            n += float(i)
        except(ValueError):
            n += 0

    return n


def make_list_post(post):
    new_list = []
    for i in post:
        try:
            time = float(post[i])
        except(ValueError):
            continue
        s = i.split(' ')
        new_list.append([s[0], s[1], s[2], time])

    return new_list
